package com.lw.sbdemo2.service;

import com.github.pagehelper.PageHelper;
import com.lw.sbdemo2.common.base.CrudService;
import com.lw.sbdemo2.common.utils.ValidUtil;
import com.lw.sbdemo2.common.vo.PageInfo;
import com.lw.sbdemo2.entity.TkBasicDict;
import com.lw.sbdemo2.entity.TkExamProductDetail;
import com.lw.sbdemo2.entity.TkExamProductMaster;
import com.lw.sbdemo2.mapper.TkBasicDictMapper;
import com.lw.sbdemo2.mapper.TkExamProductDetailMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>标题: 题库基础字典表服务 - 业务逻辑层</p>
 * <p>描述: </p>
 * <p>版权: Copyright (c) 2019</p>
 * <p>公司: 山人行工作室</p>
 *
 * @version: 1.0
 * @author: LinWang
 * @date 2019-4-14
 */
@Slf4j
@Service
public class TkBasicDictService extends CrudService<TkBasicDict, TkBasicDictMapper> {

    @Autowired
    private TkExamProductDetailMapper tkExamProductDetailMapper;

	/**
     * 获取用户分页列表
     *
     * @param query    搜索关键词
     * @param pageInfo 分页实体
	 * @param tkBasicDict 实体入参
     * @return 用户列表
     */
    public PageInfo<TkBasicDict> findListPage(String query, PageInfo pageInfo, TkBasicDict tkBasicDict) {
        PageHelper.startPage(pageInfo);
        List<TkBasicDict> tkBasicDictlist = this.dao.findListPage(query, tkBasicDict);
        return new PageInfo<>(tkBasicDictlist);
    }

    /**
     * 获取试题以及类型数据
     *
     * @param tkBasicDict 实体入参
     * @return 用户列表
     */
    public List<TkBasicDict> findListTable(String query,TkBasicDict tkBasicDict) {

        tkBasicDict.setDictName("QU_TYPE");
        String examId = tkBasicDict.getExamId();
        List<TkBasicDict> resultBasicArr = this.dao.findList(tkBasicDict);
        TkExamProductDetail tkExamProductDetail = TkExamProductDetail.builder().examId(examId).questionBody(query).build();
        List<TkExamProductDetail> resultBasic = tkExamProductDetailMapper.findList(tkExamProductDetail);

        List<TkBasicDict> resultBasicNew = CollectionUtils.isEmpty(resultBasicArr) ? null : resultBasicArr.stream()
                                            .map(temp ->{
                                                List<TkExamProductDetail> tempArr = new ArrayList<>();
                                                resultBasic.forEach(item -> {
                                                    if ((item.getQuestionType()).equals(temp.getValueCode())){
                                                        tempArr.add(item);
                                                    }
                                                    if (ValidUtil.isNotEmptyAndNull(item.getCreatedTime())) {
                                                        temp.setCreatedTime(item.getCreatedTime());
                                                    }
                                                });
                                                    temp.setExamDetails(tempArr);
                                                    return temp;
                                                }
                                            ).filter(item -> item.getExamDetails().size() > 0)
                                             .collect(Collectors.toList());
        return resultBasicNew;
    }
}